package com.yxy.controller;

import com.yxy.mapper.DepartmentMapper;
import com.yxy.mapper.EmployeeMapper;
import com.yxy.pojo.Department;
import com.yxy.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class EmployeController {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;


    @RequestMapping("/emp")
    public String list(Model model){
        List<Employee> employees = employeeMapper.employeelist();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    @RequestMapping("/toAdd")
        public String toAddpage(Model model){
            //查出部门信息
        List<Department> departments = departmentMapper.departmentList();
        model.addAttribute("departments",departments);
        return "addEmp";
    }
   @RequestMapping("/doneAdd")
    public String addEmp(Employee employee) {
        //添加操作
       employeeMapper.addEmp(employee);
       return "redirect:/emp";
    }
    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id,Model model) {
        //修改操作
        Employee employee = employeeMapper.getEmpById(id);
        model.addAttribute("emp",employee);
        List<Department> departments = departmentMapper.departmentList();
        model.addAttribute("departments",departments);
        return "updateEmp";
    }
    @RequestMapping("/doneUpdate")
    public String updateEmp(Employee employee) {
        //修改操作
       employeeMapper.updateEmp(employee);
        return "redirect:/emp";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Integer id) {
        //删除操作
        employeeMapper.deleteEmp(id);
        return "redirect:/emp";
    }
    @RequestMapping("/searchEmp")
    public String searchEmp(String lastName,Model model) {
        //删除操作
        List<Employee> employees = employeeMapper.getEmpByName(lastName);
        System.out.println(employees);
        model.addAttribute("emps",employees);
        return "searchEmp";
    }
    @RequestMapping("/comCulture")
    @ResponseBody
    public String comCulture() {
        return "还没做";
    }
}
